<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

/*
    平台游戏公告
 */

class NoticePlat extends  Model{

    const file_path =  'api/gamenotice/';
    const save_file_path =  'api/gamenotice-save/';
    const notice_file = 'operate_notice.json';

	public function paginate()
    {
    	$data = [
            "count"=>20,
            "start"=>1,
            "total"=>5,
            "subjects" =>[
                ["id" => 1, "mc" => 2000, 'name' => 'apple'],
                ["id" => 1, "mc" => 2001, 'name' => 'facebook'],
            ]
        ];
        $list = [
            ["id" => 1, "mc" => 1000, 'name' => 'white'],
            ["id" => 1, "mc" => 1001, 'name' => 'Qi360'],
            ["id" => 1, "mc" => 1002, 'name' => 'jiuyou'],
            ["id" => 1, "mc" => 1003, 'name' => 'baidu'],
            ["id" => 1, "mc" => 1004, 'name' => 'yyb'],
            ["id" => 1, "mc" => 1005, 'name' => 'xiaomi'],
            ["id" => 1, "mc" => 1006, 'name' => 'vivo'],
            ["id" => 1, "mc" => 1007, 'name' => 'oppo'],
            ["id" => 1, "mc" => 1008, 'name' => 'huawei'],
            ["id" => 1, "mc" => 1009, 'name' => 'heepay'],
            ["id" => 1, "mc" => 1010, 'name' => 'brazil'],
            ["id" => 1, "mc" => 1011, 'name' => 'wanda'],
            ["id" => 1, "mc" => 1012, 'name' => 'quick'],
            ["id" => 1, "mc" => 1014, 'name' => 'klw'],
            ["id" => 1, "mc" => 2000, 'name' => 'facebook'],
            ["id" => 1, "mc" => 2001, 'name' => 'google'],
            ["id" => 1, "mc" => 2002, 'name' => 'GameCenter'],
        ];
        
        $lang_arr = \Config::get('third.lang');
        $page = Request::get('page', 1);
        
        $page_size = count($list);

        $all_list = $before_list = [];

        $mc_arr = explode(",", env('MC', 0));
        if ($mc_arr) {
            foreach ($list as $obj) {
                if (in_array($obj['mc'] , $mc_arr)) {
                    $before_list[] = $obj;
                }
            }
            if (empty($before_list)) {
                foreach ($mc_arr as $mmc) {
                    $before_list[] = [
                        "id" => 1,
                        "mc" => $mmc,
                        "name" => $mmc,
                    ];
                }
            }
            $list = $before_list;
        }
        \Log::debug("[NOTICE_PLAT][PAGE_LIST]", [$list]);
        $notice_lang = env("NOTICE_LANG");
        if ($notice_lang) {
            $before_lang = [];
            foreach($lang_arr as $l => $obj) {
                if ($l == $notice_lang) {
                    $before_lang[$l] = $obj;
                break;
                }
            }
            $lang_arr = $before_lang;
        }
        
        $index_key = 0;
        foreach ($list as $key => $obj) {
            $sort = 0;
            foreach ($lang_arr as $l => $l_alias) {
                $obj['lang'] = $l;
                $obj['lang_alias'] = $l_alias;
                $obj['sort'] = $sort++;
        
                $all_list[$index_key++] = $obj;
            }
        }
        $last_sorts = array_column($all_list,'sort');
        array_multisort($last_sorts, SORT_ASC, $all_list);

        $total = count($all_list);

        $data_list = array_slice($all_list, ($page-1)*$page_size, $page_size);
        // extract($data);

        // $data_tmp = static::hydrate((array)$subjects);

        // $list = static::hydrate($data_tmp);

        $paginator = new LengthAwarePaginator($data_list, $total, $page_size);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }

    public static function get_init_data()
    {
        $item =  [
            "id" =>1,
            "version" =>"1.03",
            "notice_types" => "label type",
            "notice" => "",
        ];

        for ($i = 1; $i <= 3; $i++) {
            $item["id"] = $i;
            $data[(string)$i] = $item;
        }
        return $data;
    }
    public static function getOne($mc, $lang)
    {
        $file = env("APP_API_HOST", "") ."/". self::file_path.'?mc='.$mc.'&lang='.$lang;//.self::notice_file;
        $data = file_get_contents($file);
        
        $data = json_decode($data, true);
        
        if (empty($data)) {
            $data = self::get_init_data();
        }
        // 修正数据
        foreach ($data as $id =>&$obj) {
            if ($obj["id"] <= 1) {
                $obj["id"] = $id;
            }
        }

        \Log::debug(__METHOD__, [$file, $lang, $data]);
        return $data;
    }

    public static function assignData(&$target, $src)
    {
        $target['version'] = $src['version'];
        $target['notice'] = $src['notice'];
        $target['notice_types'] = $src['notice_types'];
    }

    public static function save_static($data, $mc, $lang)
    {
        //base_path(). '/'.
        $file = env("APP_API_HOST", "")."/" . self::save_file_path.$mc.'/';//.self::notice_file;
        $data_str = json_encode($data, JSON_UNESCAPED_UNICODE);
        
        //$ret = file_put_contents($file, $data_str);
        $url =  ($file .  "?data=" . $data_str);
        \Log::debug(__METHOD__,[$data, $file, $lang]);
        //redirect($url);
        //\Curl::to($file)
        //        ->asJsonRequest()
        //        ->withData([
        //            'data' => $data_str,
        //        ])
        //        ->withTimeout(60)
        //        ->asJsonResponse()
        //        ->returnResponseObject();
        //$res = $curl->post();

        //$http = new \GuzzleHttp\Client();
        $rtn = \App\Third\Curl::HttpPost($file, ['mc'=>$mc,'data'=> $data_str, 'lang'=>$lang]); 
        \Log::debug(__METHOD__, [$rtn]);
    }
}
